package com.example.chenhuamou.freenews.view.video;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.chenhuamou.freenews.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenhuamou on 2017/9/14.
 */

public class VideoFragment extends android.support.v4.app.Fragment {

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @OnClick(R.id.floatBtn)
    public void OnClicked(View view) {
        mRecyclerView.smoothScrollToPosition(0);
    }

    private void initView() {
        String[] data = {"萝卜青菜，各有所爱萝卜青菜，各有所爱萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱",
                "萝卜青菜，各有所爱萝卜青菜，各有所爱"

        };

        GridLayoutManager glm = new GridLayoutManager(getActivity(), 2);
        mRecyclerView.setLayoutManager(glm);
        mRecyclerView.setAdapter(new MyAdapter(data));
    }


    private class MyAdapter extends RecyclerView.Adapter {
        private String[] data;

        public MyAdapter(String[] data){
            this.data = data;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_video_list, parent, false);
            ViewHolder viewHolder = new ViewHolder(view);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder vh =  (ViewHolder) holder;
            vh.tv_title.setText(data[position]);
        }


        @Override
        public int getItemCount() {
            return data.length;
        }

        // 自定义 viewHolder
        public class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView img_cover;
            public TextView tv_title;
            public ImageView img_avatar;
            public TextView tv_name;

            public ViewHolder(View view) {
                super(view);
                img_cover = (ImageView) view.findViewById(R.id.img_cover);
                tv_title = view.findViewById(R.id.tv_title);
                img_avatar = view.findViewById(R.id.img_avatar);
                tv_name = view.findViewById(R.id.tv_name);
            }
        }
    }

}

